package net.melon.uploader.listener;

import javax.servlet.http.HttpSession;

import net.esj.basic.core.engine.util.logging.Logger;
import net.esj.basic.core.engine.util.logging.LoggerFactory;
import net.melon.uploader.UploadCounter;
import net.melon.uploader.Uploader;

import org.apache.commons.fileupload.ProgressListener;

public class CountPercentListener implements ProgressListener {

	private HttpSession session;
	
	private static final Logger log = LoggerFactory
			.getLogger(CountPercentListener.class);

	public CountPercentListener(HttpSession session) {
		super();
		this.session = session;
	}

	@Override
	public void update(long arg0, long arg1, int arg2) {
		UploadCounter uc = (UploadCounter) session.getAttribute(Uploader.SYS_UPLOAD_COUNTER_SESSION_KEY);
		if(uc==null){
			uc = new UploadCounter(arg1);
		}
		uc.setFileSize(arg1);
		uc.setCurrentSize(arg0);
		session.setAttribute(Uploader.SYS_UPLOAD_COUNTER_SESSION_KEY, uc);
		log.debug("update uploader percent :"+uc.getPercent());
	}

}
